-- 数据库同步服务
-- Author: Colocasia
-- Date: 2018-3-15

local skynet = require "skynet";
require "skynet.manager";

local us_db_sync = {}
local queue = {}

local function sync_impl()
    while true do
        for k, sql in pairs(queue) do
            skynet.call("usmysqlpool", "lua", "execute", sql, true);
            queue[k] = nil;
        end
        skynet.sleep(500);
    end
end

function us_db_sync.sync(sql)
    table.insert(queue, sql);
    return 0;
end

function us_db_sync.start()

end

function us_db_sync.init()
    skynet.dispatch("lua", function(session, source, cmd, ...)
        local f = assert(us_db_sync[cmd], "us_db_sync cmd=>"..cmd.." not found");
        skynet.retpack(f(...));
    end)
    skynet.fork(sync_impl);
    skynet.register(SERVICE_NAME);
end

skynet.start(us_db_sync.init);

